package com.wn.cloud.common.exception;

import com.wn.cloud.common.constant.ResponseEnum;
import com.wn.cloud.common.response.CommonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

/**
 * @FileName: GlobalExceptionHandle
 * @Description: TODO
 * @Author: MaWenHao
 * @Created: 2020/3/8 - 1:01
 * @Version: V1.0
 * @Company: 东方微银科技（北京）有限公司
 */
@Slf4j
@RestControllerAdvice
@ResponseBody
public class GlobalExceptionHandle {

    @ExceptionHandler(GlobalException.class)
    public CommonResult globalExceptionHandler(HttpServletRequest request, GlobalException e) {
        StringBuilder sb = new StringBuilder()
                .append("处理过程出现异常，Code: ")
                .append(e.getResponseEnum().getCode())
                .append(",message: ")
                .append(e.getResponseEnum().getMessage())
                .append(",请求Url: ")
                .append(request.getRequestURL());
        log.error(sb.toString(), e);

        return CommonResult.genMessageResult(e.getResponseEnum());
    }

    @ExceptionHandler(Exception.class)
    public CommonResult exceptionHandler(HttpServletRequest request, Exception e) {
        StringBuilder sb = new StringBuilder()
                .append("处理过程出现异常. ")
                .append(",请求Url: ")
                .append(request.getRequestURL())
                .append("\n")
                ;
        log.error(sb.toString(), e);

        return CommonResult.genMessageResult(ResponseEnum.UNKNOWN_EXCEPTION);
    }
}
